<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 用户管理的控制器
 */
class Stock extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		if(empty($_SESSION['id'])){
	      echo "0";
	      echo "<script src='/static/js/jquery.min.js?v=2.1.4'></script>";
	      echo "<script src='/static/js/layer/layer.js'></script>";
	      echo "<script>layer.msg('你还没有登录，请先登录',{icon:2});setTimeout(function(){window.location.href='/login';},1500)</script>";
	      exit;
	    }
	}
	//添加进货的方法
	public function addStock(){
		$data = $this->input->post();
		$data['checkout'] == 2?$data['is_payment'] = 1:$data['is_payment'] = 2;
		$data['alltotal'] = $data['price']*$data['number'] + $data['tax'];
		$data['checkout'] == 2?$data['stillneedtopay'] = $data['alltotal']:$data['stillneedtopay'] = 0;
		$data['time'] = time();
		$data['operator'] = $_SESSION['name'];
		if($this->stock->add($data)){
			$reg = array(
				"status"=>'success',
				"reson"=>"添加成功"
			);
		}else{
			$reg = array(
				"status"=>'error',
				"reson"=>"添加失败"
			);
		}
		jEcho($reg);
	}

	//修改页面数据
	public function edit(){
		$id = $this->input->get('id');
		$data = $this->stock->getOrderOne(array('id'=>$id));
		if($data){
			jechos($data);
		}else{
			jechof("没有订单信息");
		}
	}

	//修改信息的方法
	public function editStock(){
		$purview  = 2;
		if($_SESSION['usertype']>$purview){
			jechof("你没有权限访问");
		}
		$data = $this->input->post();
		$data['price1'] = $data['price'];
		$data['number1'] = $data['number'];
		$data['total'] = $data['price1']*$data['number1'];
		$data['alltotal'] = $data['total'] + $data['tax'];
		$data['checkout'] == 2?$data['is_payment'] = 1:$data['is_payment'] = 2;
		$data['checkout'] == 2?$data['stillneedtopay'] = $data['alltotal']:$data['stillneedtopay'] = 0;
		$data['etime'] = date("Y-m-d H:i:s",time());
		unset($data['price']);
		unset($data['number']);
		$num = $this->stock->getEditNum(array('id'=>$data['id']));
		$data['editnumber'] = $num['editnumber']+1;
		$data['lasteditoperator'] = $num['lasteditoperator'].$_SESSION['name'].";";
		if($this->stock->edit($data)){
			jechos("修改成功");
		}else{
			jechof("修改失败");
		}

		// if($data['oper'] == 'edit'){
		// 	$cate = $this->cate->getOne(array('name'=>$data['name']));
		// 	if(!$cate){
		// 		$reg = array(
		// 			"status"=>'error',
		// 			"reson"=>"种类错误，请查询种类名称后再修改"
		// 		);
		// 		jEcho($reg);
		// 		exit;
		// 	}
		// 	$name = $this->user->listOne(array('name'=>$data['username'],'type'=>2));
		// 	if(!$name){
		// 		$reg = array(
		// 			"status"=>'error',
		// 			"reson"=>"供货商不存在"
		// 		);
		// 		jEcho($reg);
		// 		exit;
		// 	}
		// 	$data['uid'] = $name['id'];
		// 	$data['cid'] = $cate['id'];
		// 	$where = array(
		// 		'id'=>$data['id']
		// 	);
		// 	$num = $this->stock->getEditNum($where);

		// 	$data['editnumber'] = $num['editnumber']+1;
			
		// 	$data['price1'] = $data['price'];
		// 	$data['number1'] = $data['number'];
		// 	$data['etime'] = date("Y-m-d H:i:s",time());
		// 	unset($data['username']);
		// 	unset($data['name']);
		// 	unset($data['time']);
		// 	unset($data['price']);
		// 	unset($data['number']);
		// 	unset($data['oper']);
		// 	$data['total'] = $data['price1']*$data['number1'];
		// 	$data['alltotal'] = $data['total']+$data['tax'];
		// 	$data['lasteditoperator'] = $num['lasteditoperator']."吴国良;";
		// 	if($this->stock->edit($data)){
		// 		$reg = array(
		// 			"status"=>'success',
		// 			"reson"=>"修改订单成功"
		// 		);
		// 	}else{
		// 		$reg = array(
		// 			"status"=>'error',
		// 			"reson"=>"修改订单失败"
		// 		);
		// 	}
		// }else{
		// 	$reg = array(
		// 		"status"=>'error',
		// 		"reson"=>"非法请求"
		// 	);
		// }
		// jEcho($reg);
	}

	/**
	 * 获取进货列表的方法
	 */
	public function lists(){
		$purview  = 2;
		if(@!$_SESSION['id']){
			jechof("你还没有登录，请先登录");
			exit;
		}
		if(@$_SESSION['usertype']>$purview){
			jechof("你没有权限访问");
			exit;
		}
		$rows = $this->input->post('rows');
		$page = $this->input->post('page');
		$data = $this->stock->lists();
		if($data){
			foreach ($data as $k => $v) {
				if($data[$k]['price1']){
					$data[$k]['price'] = $data[$k]['price1'];
				}
				if($data[$k]['number1']){
					$data[$k]['number'] = $data[$k]['number1'];
				}
				$data[$k]['time'] = date("Y-m-d",$v['time']);
				$data[$k]['is_payment'] == 1?$data[$k]['is_payment']="未付款":$data[$k]['is_payment']="已付款";
				if($data[$k]['lasteditoperator']){
					$arr = explode(";",$data[$k]['lasteditoperator']);
					$count = count($arr) - 2;
					$data[$k]['lasteditoperator'] = $arr[$count];
				}
				$data[$k]['caozuo'] = '<div class="btn-group">
                                <button data-toggle="dropdown" class="btn btn-warning dropdown-toggle" aria-expanded="false">操作 <span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu">
                                    <li><a onclick="edit('.$v["id"].')" >修改</a>
                                    </li>
                                    <li class="divider"></li>
                                    <li><a href="javascript:void(0);">删除</a>
                                    </li>
                                </ul>
                            </div>';
			}
			
			jechos($data);
		}else{
			jechof("获取进货列表失败");
		}
	}

	/**
	 * 搜获获取进货列表的方法
	 */
	public function searchLists(){

		$stime = strtotime($this->input->post("stime"));
		$etime = strtotime($this->input->post("etime"));
		$rows = $this->input->post('rows');
		$page = $this->input->post('page');
		if($etime){
			$where = "s.time between ".$stime." and ".$etime;
		}else{
			$where = "s.time >".$stime;
		}
		
		$data = $this->stock->searchList($where,$rows,$page);
		$total = $this->stock->searchListNum($where);
		if($data){
			foreach ($data as $k => $v) {
				$data[$k]['time'] = date("Y-m-d",$v['time']);
				$data[$k]['priceTotal'] = $v['price']*$v['number'];
			}
			$reg = array(
				"status"=>'success',
				'total'=>ceil($total/$rows),
				"rows"=>$data
			);
		}else{
			$reg = array(
				"status"=>'error',
				"reson"=>"获取进货列表失败"
			);
		}
		jEcho($reg);
	}
}